package com.liferay;

import java.io.IOException;
import java.io.PrintWriter;

import java.util.ArrayList;
import java.util.Enumeration;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import com.liferay.outil.OaUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.Group;
import com.liferay.portal.model.User;
import com.liferay.portal.service.GroupLocalServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.sun.xml.internal.fastinfoset.sax.Properties;


public class online extends GenericPortlet {

	public void init() throws PortletException {
		editJSP = getInitParameter("edit-jsp");

		// Help Mode Pages
		helpJSP = getInitParameter("help-jsp");

		// View Mode Pages
		viewJSP = getInitParameter("view-jsp");

		
	}

	public void doDispatch(RenderRequest renderRequest, RenderResponse renderResponse)throws IOException, PortletException{

		String jspPage = renderRequest.getParameter("jspPage");

		if (jspPage != null){
			 include(jspPage, renderRequest, renderResponse);
		}
		else{
			 super.doDispatch(renderRequest, renderResponse);
		}
	}

	public void doEdit(RenderRequest renderRequest, RenderResponse renderResponse)throws IOException, PortletException {
		if (renderRequest.getPreferences() == null) {
			super.doEdit(renderRequest, renderResponse);
		}
		else {
			include(editJSP, renderRequest, renderResponse);
		}
	}

	public void doHelp(RenderRequest renderRequest, RenderResponse renderResponse)throws IOException, PortletException{
		include(helpJSP, renderRequest, renderResponse);
	}

	public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException
	  {
		  include(viewJSP, renderRequest, renderResponse);
	 }
    public void processAction(ActionRequest actionRequest, ActionResponse actionResponse)throws IOException, PortletException 
	  { System.out.println("hhhhhhhhhhhhhhhhhhhhh");
  	  String enteredCaptchaText = ParamUtil.getString(actionRequest, "captchaText");
  	  PortletSession session = actionRequest.getPortletSession();
       String captchaText = (String)getCaptchaValueFromSession(session);
       if (Validator.isNull(captchaText)) {
      	 //actionRequest.setAttribute("AjexResponse", "<CodeValidation>Invalid<CodeValidation>");
           actionResponse.setRenderParameter("captchaResult", "Invalid");
           setValues(actionRequest,actionResponse);
      }
      //if (!StringUtils.equals(captchaText, enteredCaptchaText)) {
       else if (!captchaText.equals(enteredCaptchaText)) {
      	 //actionRequest.setAttribute("AjexResponse", "<CodeValidation>Invalid<CodeValidation>");
      	 actionResponse.setRenderParameter("captchaResult", "Invalid");
      	 setValues(actionRequest,actionResponse);
      }  
       else{
  	   	  
    	   
  	  ThemeDisplay themeDisplay= (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
  	   String name = ParamUtil.getString(actionRequest, "YourName");
  	   String Email = ParamUtil.getString(actionRequest, "EmailId");
  	   String Address = ParamUtil.getString(actionRequest, "Address");
  	   String Contact = ParamUtil.getString(actionRequest, "MobileNo");
  	   String Perpose = ParamUtil.getString(actionRequest, "Purpose");
  	   long hdn = ParamUtil.getLong(actionRequest, "hdn");
  	   System.out.println(name+""+Email+""+Address+""+Contact+""+Perpose+""+hdn);
  	 
   	
  	   User u = null;
  	 try {
		 u =     UserLocalServiceUtil.getUser(hdn);
	} catch (PortalException e2) {
		// TODO Auto-generated catch block
		e2.printStackTrace();
	} catch (SystemException e2) {
		// TODO Auto-generated catch block
		e2.printStackTrace();
	}
  	
		if(u != null)
		{	
  	   
  	   String strTitle = "your online appointment send to Dr "+u.getFullName();
  	   String strBody = "<table>" +
  	   		"<tr><td> We had send your following request to Dr. " +u.getFullName() +" </td></tr>" +
  	   		"<tr><td> " + Perpose +" </td></tr>" +
  	   		"</table>";
  	  
     	   try{
     		   OaUtil.sendEMail("MedicalAssociation", "noreply@medicalassociation.in", name, Email, strTitle, strBody);
     		   SessionMessages.add(actionRequest,"success");
     		}
 		 	catch(Exception e){
 				 e.printStackTrace();
 		}
  	   
  	   String mailaddresses = u.getEmailAddress() ;
  	   String strTitle1 = "Online appointment from medcalassociation.in ";
  	   String strBody1 = "<table>" +
  	   		"<tr><td colspan=\"2\" > " + name +" want to contact you for </td></tr>" +
  	   		"<tr><td width=\"100px\"><b>  Perpose :</b> </td> <td> " + Perpose +" </td></tr>" +
  	   		"<tr><td width=\"100px\"><b>  Full Name :</b> </td> <td> " + name +" </td></tr>" +
  	   		"<tr><td width=\"100px\"><b>  Email : </b></td> <td> " + Email +" </td></tr>" +
  	   		"<tr><td width=\"100px\"><b>  Contact : </b></td> <td> " + Contact +" <br>"+ Address +"</td></tr>" +
  	   		"</table>";
  	 	try
		 	{
  	 		String strDispUrl=u.getDisplayURL(themeDisplay);
  	 		OaUtil.sendEMailToMember("MedicalAssociation", "noreply@medicalassociation.in", strTitle1, strBody1, u, strDispUrl);
  	 		actionResponse.setRenderParameter("msg-send-successfully", "Yes");
  	 	}
		 	catch(Exception e){
				 e.printStackTrace();
		}  
  	 	
		}   
		
       }
      }
       public void setValues(ActionRequest request, ActionResponse response)
       {
     	response.setRenderParameter("FullName", ParamUtil.getString(request, "yourname"));
     	response.setRenderParameter("strEmail", ParamUtil.getString(request, "Email"));
     	response.setRenderParameter("strAddress", ParamUtil.getString(request, "Address"));
     	response.setRenderParameter("strContact", ParamUtil.getString(request, "Contact"));
     	response.setRenderParameter("strPurpose", ParamUtil.getString(request, "Perpose"));
       }
    public void serveResource(ResourceRequest resourceRequest,
            ResourceResponse resourceResponse) throws IOException, PortletException {
    	try {
				 com.liferay.portal.kernel.captcha.CaptchaUtil.serveImage(resourceRequest, resourceResponse);
							
			     
			//CaptchaUtil.getCaptcha().toString();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("In captch");
		}
    }
    private String getCaptchaValueFromSession(PortletSession session) {
        Enumeration<String> atNames = session.getAttributeNames();
        while (atNames.hasMoreElements()) {
            String name = atNames.nextElement();
            if (name.contains("CAPTCHA_TEXT")) {
                return (String) session.getAttribute(name);
            }
        }
        return null;
    } 
    protected void include(String path, RenderRequest renderRequest,RenderResponse renderResponse)throws IOException, PortletException 
	  {
		PortletRequestDispatcher portletRequestDispatcher =getPortletContext().getRequestDispatcher(path);

		if (portletRequestDispatcher == null) 
		   {
			  _log.error(path + " is not a valid include");
		   }
		else 
		   {
			 portletRequestDispatcher.include(renderRequest, renderResponse);
		   }
	  }

	protected String editJSP;
	protected String helpJSP;
	protected String viewJSP;

	private static Log _log = LogFactoryUtil.getLog(online.class);

}